package com.qf.kaohe.handler;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.kaohe.pojo.TUser;
import com.qf.kaohe.service.TUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class TUserHandler {
    @Resource
    TUserService service;
    @RequestMapping("/users")
    @ResponseBody
    public PageInfo allUsers(@RequestParam(value = "pn" ,defaultValue = "1") Integer pn){
        PageHelper.startPage(pn,10);
        List<TUser> TUsers = service.allTUserPage();
        PageInfo<TUser> pageInfo = new PageInfo<>(TUsers,10); // navigatePages：一次性要显示多少页
        return pageInfo;
    }
    @RequestMapping("/selectByInput")
    @ResponseBody
    public List<TUser> selectByInput(
            @RequestParam("name") String name,
            @RequestParam("type") String type,
            @RequestParam("isDelete") String isDelete
    ){;
        if (isDelete.equals("正常")){
            Integer isDele = 0;
            List<TUser> list = service.selectByInput(name,type,isDele);
            return list;
        }else {
            Integer isDele = 1;
            List<TUser> list = service.selectByInput(name,type,isDele);
            return list;
        }
    }
    @RequestMapping("/insert")
    @ResponseBody
    public String insert(@RequestBody TUser tUser){
        Integer res = service.insert(tUser.getName(),tUser.getType(),tUser.getNote());
            if (res==1){
                return "Y";
            }else {
                return "N";
            }
    }
    @RequestMapping("/selectById")
    @ResponseBody
    public TUser selectById(Integer id){
        TUser tuser = service.selectById(id);
        System.out.println(tuser);
        return tuser;
    }
    @RequestMapping("/update")
    @ResponseBody
    public String updateByData(@RequestBody TUser tUser){
        Integer res = service.updateByData(tUser.getId(),tUser.getName(),tUser.getType(),tUser.getNote());
        if (res==1){
            return "Y";
        }else {
            return "N";
        }
    }
    @RequestMapping("/deleteById")
    @ResponseBody
    public String deleteById(Integer id){
        System.out.println(id);
        Integer res = service.deleteById(id);
        if (res == 1){
            return "Y";
        }
        return "N";
    }
    @RequestMapping("/excelOut")
    @ResponseBody
    public void excelOut() {
        String filePath = "D:\\ideaWorkSpace\\KaoHe\\src\\main\\resources\\类型管理.xls";
        List<TUser> TPhysicalDistributionManagement = service.allTUserPage();
        ExcelWriter excelWriter = EasyExcel.write(filePath,TUser.class).build();
        WriteSheet writeSheet = EasyExcel.writerSheet("类型").build();
        excelWriter.write(TPhysicalDistributionManagement,writeSheet);
        excelWriter.finish();
    }
}
